package com.cy.personer.web;

import com.cy.personer.bean.*;
import com.cy.personer.biz.ComboBiz;
import com.cy.personer.biz.ReportBiz;
import com.cy.personer.biz.WorkBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.cy.personer.util.BarcodeUtil.generateFile;

/**
 * 报告
 * @author 林少艺
 */
@Controller
@RequestMapping("/report")
public class ReportHandler {

    @Resource
    ReportBiz reportBizImpl;
    @Resource
    WorkBiz workBizImpl;
    /**
     * 根据套餐新增报告
     * @param combo
     * @param session
     * @return
     * @author 林少艺
     */
    @RequestMapping(value = "/addReportCombo.action", method= RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody public String addReportCombo(Combo combo, HttpSession session){
        Work work = (Work)session.getAttribute("work");

        User user = (User)session.getAttribute("user");
        String str = reportBizImpl.addReportCombo(user,combo,work);
        return str;
    }

    /**
     * 手机根据套餐新增报告
     * @param combo
     * @param session
     * @return
     * @author 林少艺
     */
    @RequestMapping(value = "/phAddReportCombo.action", method= RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody public String phAddReportCombo(Combo combo, HttpSession session){
        Work work=workBizImpl.getWork(new Work()).get(0);
        User user = (User)session.getAttribute("frontUser");
        String str = reportBizImpl.addReportCombo(user,combo,work);
        return str;
    }

    /**
     * 手机根据自选项目新增报告
     * @param itemIds
     * @param session
     * @return
     * @author 林少艺
     */
    @RequestMapping(value = "/phAddReportItem.action", method= RequestMethod.POST, produces="text/html;charset=UTF-8")
    public @ResponseBody String phAddReportItem(@RequestParam("itemIds")int[] itemIds, HttpSession session){
        Work work=workBizImpl.getWork(new Work()).get(0);
        User user = (User)session.getAttribute("frontUser");
        String str = "" + reportBizImpl.addReportItem(user,itemIds,work);
        return str;
    }

    /**
     * 根据自选项目新增报告
     * @param itemIds
     * @param session
     * @return
     * @author 林少艺
     */
    @RequestMapping(value = "/addReportItem.action", method= RequestMethod.POST, produces="text/html;charset=UTF-8")
    public @ResponseBody String addReportItem(@RequestParam("itemIds")int[] itemIds, HttpSession session){
        Work work = (Work)session.getAttribute("work");
        User user = (User)session.getAttribute("user");
        String str = "" + reportBizImpl.addReportItem(user,itemIds,work);
        return str;
    }

    /**
     * 查找报告项目
     * @param report
     * @return
     * @author 林少艺
     */
    @RequestMapping("/getReportItem.action")
    public @ResponseBody Report getReportItem(Report report){
        Report report1 = reportBizImpl.getReportItem(report);
        return report1;
    }

    /**
     * 导检单打印页面
     * @author 林少艺
     */
    @RequestMapping("/report_guide_print.action")
    public ModelAndView reportGuidePrint(Report report){
        Report report1 =reportBizImpl.getReport(report);
        ModelAndView mov=new ModelAndView();
        mov.addObject("report",report1);
        mov.setViewName("admin/report_guide_print");
        return mov;
    }

    /**
     * 手机导检单页面
     * @author 林少艺
     */
    @RequestMapping("/report_guide.action")
    public ModelAndView reportGuide(Report report){
        Report report1 =reportBizImpl.getReport(report);
        ModelAndView mov=new ModelAndView();
        mov.addObject("report",report1);
        mov.setViewName("user/report_guide");
        return mov;
    }

    /**
     * 报告打印页面
     * @author 林少艺
     */
    @RequestMapping("/report_print.action")
    public ModelAndView reportPrint(Report report,HttpSession session){
        Report report1 =reportBizImpl.getReport(report);
        String path = session.getServletContext().getRealPath("/barcode");
        File folder = new File(path);// 目录
        String keyword = ""+report.getReportId()+".png";//文件名
        //目录不存在
        if  (!folder .exists()  && !folder .isDirectory())
        {
            folder .mkdir();
            generateFile(report.getReportId(), path+"/"+keyword);
            //目录存在
        } else {
            File folder1 = new File(path+"/"+keyword);// 目录
            //文件不存在
            if(!folder1.exists())
            {
                generateFile(report.getReportId(), path+"/"+keyword);
            }
        }
        ModelAndView mov=new ModelAndView();
        mov.addObject("report",report1);
        mov.setViewName("admin/report_print");
        return mov;
    }

    /**
     * 手机报告页面
     * @author 林少艺
     */
    @RequestMapping("/report.action")
    public ModelAndView report(Report report,HttpSession session){
        Report report1 =reportBizImpl.getReport(report);
        ModelAndView mov=new ModelAndView();
        mov.addObject("report",report1);
        mov.setViewName("user/report");
        return mov;
    }
}
